package it.sd.pwframework.persistence.model.spider;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

import org.hibernate.annotations.Sort;
import org.hibernate.annotations.SortType;

@Entity
@Table(name="pwservice")
@XmlRootElement(namespace="it.sd.pwframework.pswervice")
public class PWService {

	@Id
	@GeneratedValue
	private Long id;
	
	private String code;
	private String label;
	private String description;
	
	@OneToMany(fetch=FetchType.EAGER)
	@Sort(type=SortType.NATURAL)
    private SortedSet<PWStep> steps = new TreeSet<PWStep>();    

	@OneToMany(fetch=FetchType.EAGER)
    private List<PWParam> inputParams= new LinkedList<PWParam>();    

    public PWStep getFirstStep() {
    	return steps.first();
    }
    
    public PWStep getStep(String id) {
    	PWStep step = null;
    	
    	for (Iterator<PWStep> iterator = steps.iterator(); iterator.hasNext();) {
			PWStep s = iterator.next();
			if (s.getName().equals(id)) {
				step = s;
				break;
			}
			
		}
    	
    	return step;
    }

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public SortedSet<PWStep> getSteps() {
		return steps;
	}

	public void setSteps(SortedSet<PWStep> steps) {
		this.steps = steps;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public List<PWParam> getInputParams() {
		return inputParams;
	}

	public void setInputParams(List<PWParam> inputParams) {
		this.inputParams = inputParams;
	}

	
	

    
    
}
